#include "widget.h"
#include "ui_widget.h"

#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //启动两个定时器，函数的返回结果是定时器的id
    //函数的参数是定时器的时间间隔
    m_id1 = startTimer(500);
    m_id2 = startTimer(1000);

    //第二种定时器的用法：使用QTimer类型
    QTimer *tm = new QTimer(this);

    connect(tm, &QTimer::timeout, [=]{
        static int num3 = 1;
        ui->label_3->setText(QString::number(num3++));
    });

    tm->start(2000);//启动定时器

}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *event)
{
    //根据定时器的id进行判断到底走哪一个分支
    if(m_id1 == event->timerId())
    {
        static int num1 = 1;
        ui->label->setText(QString::number(num1++));
    }

    if(m_id2 == event->timerId())
    {
        static int num2 = 1;
        ui->label_2->setText(QString::number(num2++));
    }
}

